package com.ykht.proj.activity.timer;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.activity.Enum.AgreementStatusEnum;
import com.ykht.proj.activity.constant.ActivityConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.mq.producer.MsgProducer;
import com.ykht.proj.activity.service.*;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.constant.SecurityConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.comsrv.constant.ComConstant;
import com.ykht.proj.comsrv.vo.SubscribeMsgCardExpire;
import com.ykht.proj.comsrv.vo.SubscribeMsgCouponExpire;
import com.ykht.proj.coupon.api.feign.RemotePersonService;
import com.ykht.proj.coupon.entity.CnPerson;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@SuppressWarnings("all")
@Slf4j
@EnableScheduling
public class CardTimer {

	@Resource
	private MsgProducer msgProducer;
	@Resource
	private RemotePersonService remotePersonService;

	@SneakyThrows
	@Scheduled(cron = "0 0 8 * * ?")
	public void  vInfoExpire4SubMsg() {
		log.info("->定时任务：会员卡过期前三天消息提醒");
		List<VInfo> vInfoList = new VInfo().selectList(Wrappers.lambdaQuery(VInfo.class).between(VInfo::getEndTime,LocalDateTime.now(),LocalDateTime.now().plusDays(3)));
		if (vInfoList.size()>0){

			List<Long> cardIds = vInfoList.stream().map(o->o.getCardId()).collect(Collectors.toList());
			String ids = vInfoList.stream().map(o->String.valueOf(o.getPersonId())).collect(Collectors.joining(","));
			List<VCard> vCards = new VCard().selectList(Wrappers.lambdaQuery(VCard.class).in(VCard::getId,cardIds));

			R<List<CnPerson>> r = remotePersonService.byIdsFromIn(ids, SecurityConstants.FROM_IN);
			if (r.getCode()== CommonConstants.SUCCESS){
				List<CnPerson> personList = r.getData();
				if (personList!=null&&personList.size()>0){
					for (VInfo o : vInfoList) {
						SubscribeMsgCardExpire cardExpire = new SubscribeMsgCardExpire();
						cardExpire.setType(ComConstant.SUBSCRIBE_MSG_CARD_EXPIRE);
						cardExpire.setPersonId(o.getPersonId());
						cardExpire.setVId(o.getId());
						vCards.stream().filter(oo->oo.getId().equals(o.getCardId())).findFirst().ifPresent(card->{
							cardExpire.setCardName(card.getName());
							cardExpire.setContent("您的会员卡即将到期请尽快使用！");
							cardExpire.setStartTime(o.getStartTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
							cardExpire.setEndTime(o.getEndTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
						});
						CnPerson person = personList.stream().filter(oo->oo.getId().equals(o.getPersonId())).findFirst().get();
						if (person!=null&&person.getOpenId()!=null){
							cardExpire.setOpenId(person.getOpenId());
							msgProducer.sendMsg(cardExpire);
						}
					}
				}
			}
		}

	}

	/**
	 * 每天0点过期
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void  vInfoExpire() {
		log.info("->定时任务：会员卡 自动过期");
		new VInfo().update(Wrappers.lambdaUpdate(VInfo.class).set(VInfo::getStatus,1).eq(VInfo::getStatus,0).lt(VInfo::getEndTime,LocalDateTime.now()));
	}

}
